package com.kedi.productcenter.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.kedi.productcenter.enums.ResultEnum;
import com.kedi.productcenter.exception.CommonException;
import com.kedi.productcenter.service.BlsProductService;
import com.kedi.productcenter.utils.ResultVoUtil;
import com.kedi.productcenter.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;

/**
* @Description:    商品控制层
* @Author:         ytw
* @CreateDate:     2019/7/12 10:32
* @UpdateRemark:   修改内容
* @Version:        1.0
*/
@RestController
public class BlsController {
    private static Logger logger = LoggerFactory.getLogger(BlsController.class);
    @Autowired
    private BlsProductService blsProductService;

    /**
    * 方法实现说明 获取所有作品
    * @author      ytw
    *@param         page,size
    * @return
    * @exception
    * @date        2019/7/11 11:27
    */
    @RequestMapping(value="/opus/list", method = RequestMethod.GET)
    public Map<String, Object> getOpusList(@RequestParam(value="page",defaultValue = "1")int page,
                                                @RequestParam(value="size",defaultValue = "20")int size){
        PageHelper.startPage(page,size);
        List<Map<String, Object>> list = blsProductService.findList();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        Map<String, Object> data = Maps.newHashMapWithExpectedSize(20);
        data.put("total_size",pageInfo.getTotal());
        data.put("total_page",pageInfo.getPages());
        data.put("current_page",page);
        data.put("data",pageInfo.getList());
        if(list.isEmpty()){
            logger.info("查找所有比南山作品列表为空");
            throw new CommonException(ResultEnum.LIST_NOT_EXISTED);
        }else{
            return data;
        }
    }

    /**
    * 方法实现说明  根据状态获取商品列表
    * @author      ytw
    *@param         page,size,num   0表示删除状态 1表示草稿状态 2表示待审核状态 3表示上线状态
    * @return      list
    * @exception
    * @date        2019/7/11 11:27
    */
    @RequestMapping(value = "/opus/partlist", method = RequestMethod.GET)
    Map<String, Object> getOpusPartList(@RequestParam(value="page",defaultValue = "1")int page,
                                             @RequestParam(value="size",defaultValue = "20")int size,@RequestParam("num") Integer num){
        PageHelper.startPage(page,size);
        List<Map<String, Object>> list = blsProductService.findbyStatus(num);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        Map<String, Object> data = Maps.newHashMapWithExpectedSize(20);
        data.put("total_size",pageInfo.getTotal());
        data.put("total_page",pageInfo.getPages());
        data.put("current_page",page);
        data.put("data",pageInfo.getList());
        if(list.isEmpty()){
            logger.info("查找所有比南山作品列表为空");
            throw new CommonException(ResultEnum.LIST_NOT_EXISTED);
        }else{
            return data;
        }
    }

    /**
    * 方法实现说明  获取商品详情
    * @author      ytw
    *@param         opusid,puserid
    * @return
    * @exception
    * @date        2019/7/11 11:28
    */
    @RequestMapping(value="/opus/detail", method = RequestMethod.GET)
    Map<String,Object> getOpusDetail(@RequestParam("opusid") Integer opusid,@RequestParam("puserid") Integer puserid){
        if(opusid==null&&puserid==null){
            logger.error("查找所有比南山作品详情所传商品id为空");
            throw new CommonException(ResultEnum.PARAMETER_EXISTED);
        }
        Map<String, Object> map = blsProductService.findDetail(opusid,puserid);
        if(map.isEmpty()){
            logger.info("查找所有比南山作品详情为空");
            throw new CommonException(ResultEnum.LIST_NOT_EXISTED);
        }else{
            return map;
        }
    }

    /**
     * 方法实现说明   商品提交申请 插入数据库
     * @author      ytw
     *@param         puserid,name,price,weight,uploadUser,address
     * @return
     * @exception
     * @date        2019/7/11 11:28
     */
    @RequestMapping(value="/opus/createopus", method = RequestMethod.POST)
    ResultVo createopus(@RequestParam("puserid") Integer puserid, @RequestParam("name") String name,
                        @RequestParam("price") Integer price,
                        @RequestParam("weight") Float weight, @RequestParam("uploadUser") Integer uploadUser,
                        @RequestParam("communitycid")Integer communitycid){
        Map<String, Object> map = Maps.newHashMapWithExpectedSize(10);
        map.put("puserid",puserid);
        map.put("name",name);
        map.put("price",price);
        map.put("weight",weight);
        map.put("uploadUser",uploadUser);
        map.put("opus_id",-1);
        map.put("communityc_id",communitycid);
        Integer result = blsProductService.createOpus(map);
        if(result!=1){
            logger.info("作品提交申请失败");
            throw new CommonException(ResultEnum.SAVE_DATA_ERROR);
        }else {
            return ResultVoUtil.success();
        }
    }
    /**
     * 方法实现说明  图片上传
     * @author      ytw
     *@param        file,userId
     * @return
     * @exception
     * @date        2019/7/21 8:24
     */
    @PostMapping("/opus/postfile")
    public String fileUpload (@RequestParam(value = "file") MultipartFile file,
                              @RequestParam(value = "userId", required = false) Integer userId){
        String result = blsProductService.fileUpLoad(file, userId);
        if(result==null){
            logger.info("图片上传失败");
            throw new CommonException(ResultEnum.UPLOAD_ERROR);
        }else {
            logger.info("图片上传成功，返回图片地址");
            return result;
        }
    }

    /**
     * 方法实现说明  删除作品
     * @author      ytw
     *@param
     * @return
     * @exception
     * @date        2019/7/21 8:24
     */
    @PostMapping("/opus/deletebyopusid")
    public ResultVo deleteById(@RequestParam("opusid")Integer opusid){
        Integer result = blsProductService.deleteOpus(opusid);
        if(result!=1){
            logger.info("删除作品申请失败");
            throw new CommonException(ResultEnum.SAVE_DATA_ERROR);
        }else {
            return ResultVoUtil.success();
        }
    }
}
